package slr.comment.portlet.data;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import slr.comment.model.SlrAnchor;
import slr.comment.model.SlrDocument;
import slr.comment.model.SlrDocumentStructure;
import slr.comment.model.SlrDocumentTyp;
import slr.comment.model.SlrGraph;
import slr.comment.portlet.util.Lang;
import slr.comment.portlet.util.SLRConstants;
import slr.comment.service.SlrAnchorLocalServiceUtil;
import slr.comment.service.SlrDocumentLocalServiceUtil;
import slr.comment.service.SlrDocumentStructureLocalServiceUtil;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.portlet.PortletProps;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;
import com.vaadin.ui.VerticalLayout;

/**
 * The details corresponding to the SLR-Comment portlet are returned from this
 * class
 * 
 * @author TransIT mPower Labs
 * 
 */
@SuppressWarnings("serial")
public class SLRDetailsContent  {

	public final Table anchorTable = new Table(Lang.getKey("slr.comment.comments-list"));

	public SLRDetailsContent() {

	}

	/**
	 * The details of the corresponding Node clicked from the Hierarchial Tree
	 * is returned.
	 * 
	 * @param object
	 * @return Label containing the SLR Details.
	 */
	public Label addDetailComponent(Object object) {

		String details = "";
		if (object instanceof SlrDocumentTyp) {

			SlrDocumentTyp slrDocumentTyp = (SlrDocumentTyp) object;
			try {
				details = getString(PortletProps.get("details.slrDocTyp"));
				details = details.replace("[$docTypId$]", String.valueOf(slrDocumentTyp.getSlrDocumentTypId()));
				details = details.replace("[$docTypDesc$]", slrDocumentTyp.getSlrDocTypDesc());
				details = details.replace("[$docTyplab$]", slrDocumentTyp.getSlrDocTypLabel());
				details = details.replace("[$docTypMtitle$]", slrDocumentTyp.getSlrDocTypMainTitle());
				details = details.replace("[$docTypSTitle$]", slrDocumentTyp.getSlrDocTypSubTitle());
			} catch (Exception e) {
				_log.error(e.getMessage());
			}
			Label slrDetails = new Label(details);
			slrDetails.setHeight(400, Label.UNITS_PIXELS);
			slrDetails.setContentMode(Label.CONTENT_XHTML);
			return slrDetails;

		} else if (object instanceof SlrDocument) {
			SlrDocument slrDocument = (SlrDocument) object;

			try {
				details = getString(PortletProps.get("details.slrDocument"));
				details = details.replace("[$docTypId$]", String.valueOf(slrDocument.getSlrDocumentTypId()));
				details = details.replace("[$docId$]", String.valueOf(slrDocument.getSlrDocumentId()));
				details = details.replace("[$docDtFrm$]", String.valueOf(slrDocument.getSlrDocumentDateFrom()));
				details = details.replace("[$docDtTo$]", String.valueOf(slrDocument.getSlrDocumentDateTo()));
				details = details.replace("[$docDocLab$]", slrDocument.getSlrDocumentPeriodLabel());
			} catch (Exception e) {
				_log.error(e.getMessage());
			}
			Label slrDetails = new Label(details);
			slrDetails.setHeight("100%");
			slrDetails.setContentMode(Label.CONTENT_XHTML);
			return slrDetails;
		} else if (object instanceof SlrAnchor) {
			SlrAnchor slranchor = (SlrAnchor) object;

			try {
				details = getString(PortletProps.get("details.slrAnchor"));
				details = details.replace("[$ancId$]", String.valueOf(slranchor.getSlrAnchorId()));
				details = details.replace("[$ancLabel$]", String.valueOf(slranchor.getSlrAnchorLabel()));
				details = details.replace("[$ancHead1$]", String.valueOf(slranchor.getSlrAnchorHeader1()));
				details = details.replace("[$ancHead2$]", String.valueOf(slranchor.getSlrAnchorHeader2()));
				details = details.replace("[$ancTxt$]", slranchor.getSlrAnchorText());
				details = details.replace("[$ancDesc$]", slranchor.getSlrAnchorDesc());

			} catch (Exception e) {
				_log.error(e.getMessage());
			}

			Label slrDetails = new Label(details);
			slrDetails.setHeight("100%");
			slrDetails.setContentMode(Label.CONTENT_XHTML);

			return slrDetails;
		}
		return new Label(Lang.getKey("slr.comment.no-details-available"));
	}

	/**
	 * The List of child node Corresponding to Node clicked from Hierarchial
	 * container is added to the Table.
	 * 
	 * @param object
	 * @return Table with BeanItemContainer as Datasource.
	 */
	public Component addListComponent(Object object) {

		if (object instanceof SlrDocumentTyp) {

			SlrExplorerContainer.listName = Lang.getKey("slr.comment.list-of-documents");
			List<SlrDocument> docList = null;
			BeanItemContainer<SlrDocument> slrDocContainer = null;
			Table table = new Table();
			SlrDocumentTyp slrDocumentTyp = (SlrDocumentTyp) object;
			long slrDocId = slrDocumentTyp.getSlrDocumentTypId();
			try {
				docList = SlrDocumentLocalServiceUtil.findByDocumentTypId(slrDocId);
				if (Validator.isNotNull(docList) && docList.size() > 0) {
					slrDocContainer = new BeanItemContainer<SlrDocument>(docList);

					table.setContainerDataSource(slrDocContainer);
				} else {
					return new Label(Lang.getKey("slr.comment.list-is-empty"));
				}
			} catch (Exception e) {
				_log.error(e.getMessage());
			}

			table.setVisibleColumns(SLRConstants.SLRDOCUMENT.split(","));
			table.setImmediate(true);
			table.setHeight("100%");
			table.setWidth("100%");
			return table;
		} else if (object instanceof SlrDocument) {
			SlrExplorerContainer.listName = Lang.getKey("slr.comment.list-of-anchors");
			Table table = new Table();
			BeanItemContainer<SlrAnchor> slrDocContainer = null;
			List<SlrAnchor> slrAnchorsList = new ArrayList<SlrAnchor>();
			SlrDocument slrDocument = (SlrDocument) object;
			try {
				List<SlrDocumentStructure> slrDocumentStructureList = SlrDocumentStructureLocalServiceUtil
						.findByDocumentTypId(slrDocument.getSlrDocumentTypId());
				for (int k = 0; k < slrDocumentStructureList.size(); k++) {
					long slrAnchorId = 0l;
					if (Validator.isNotNull(slrDocumentStructureList.get(k))) {
						slrAnchorId = slrDocumentStructureList.get(k).getSlrAnchorId();
						if (Validator.isNotNull(slrAnchorId) && slrAnchorId != 0) {
							slrAnchorsList.add(k, SlrAnchorLocalServiceUtil.getSlrAnchor(slrAnchorId));
						}

					}
				}
				slrDocContainer = new BeanItemContainer<SlrAnchor>(slrAnchorsList);
				table.setContainerDataSource(slrDocContainer);
				table.setVisibleColumns(SLRConstants.SLRANCHOR.split(","));
				table.setImmediate(true);
				table.setHeight("100%");
				table.setWidth("100%");
				return table;

			} catch (Exception e) {
				_log.error(e.getMessage());
			}

		}
		return new Label(Lang.getKey("slr.comment.no-list-available"));
	}

	private InputStream getInputStream(String file) throws Exception {
		ClassLoader classLoader = getClass().getClassLoader();
		return classLoader.getResourceAsStream(file);
	}

	private byte[] getBytes(String file) throws Exception {
		return FileUtil.getBytes(getInputStream(file));
	}

	private String getString(String file) throws Exception {
		return new String(getBytes(file));
	}

	/**
	 * Gets the List of Graphs corresponding to SLRAnchor.
	 * @param slrAnchorId
	 * @param third
	 * @return Table with BeanItemContainer as Datasource.
	 */
	public Component getGraphsList(long slrAnchorId, final VerticalLayout third) {
		// TODO Auto-generated method stub
		Table table = new Table();

		List<SlrGraph> slrGraphList = new ArrayList<SlrGraph>();
		try {
			slrGraphList = SlrAnchorLocalServiceUtil.getSlrGraphs(slrAnchorId);
			if (Validator.isNull(slrGraphList) || slrGraphList.size() <= 0) {
				return new Label(Lang.getKey("slr.comment.no-graphs-available"));
			}
		} catch (SystemException e) {
			_log.error(e.getMessage());
		}
		BeanItemContainer<SlrGraph> slrGraphContainer = null;
		slrGraphContainer = new BeanItemContainer<SlrGraph>(slrGraphList);
		table.setSelectable(true);
		table.setImmediate(true);
		table.setHeight(150, Table.UNITS_PIXELS);
		table.setWidth(100, Table.UNITS_PERCENTAGE);
		table.setContainerDataSource(slrGraphContainer);
		table.setVisibleColumns(SLRConstants.SLRGRAPH.split(","));

		table.addGeneratedColumn("GraphList", new ColumnGenerator() {
			private static final long serialVersionUID = 1L;

			public Component generateCell(Table source, final Object itemId, Object columnId) {
				VerticalLayout htmlLayout = new VerticalLayout();
				Button showGraph = new Button(Lang.getKey("slr.comment.show-graph"), new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						final SLRGraphList grapList = new SLRGraphList();
						SlrGraph slrGraph = (SlrGraph) itemId;
						third.replaceComponent(third.getComponent(1), grapList.getList(slrGraph.getSlrGraphId()));
					}
				});
				htmlLayout.addComponent(showGraph);
				return htmlLayout;
			}
		});
		return table;

	}
    private static Log _log = LogFactoryUtil.getLog(SLRDetailsContent.class);

}